package com.dr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dr.pojo.Dept;
import com.dr.pojo.User;
import com.dr.service.DeptService;
import com.dr.service.UserService;
//@RestController = @Controller + @ResponseBody
@Controller
@RequestMapping("/um_ajax")
public class UserController_ajax { 
	@Autowired
	private UserService userService;//Spring注入ServiceImpl的实例
	
	@Autowired
	private DeptService deptService;//Spring注入ServiceImpl的实例
	
	@ResponseBody
	@RequestMapping("/userList")
	public Map<String,Object> userList(User user) {
		Map<String,Object> map = new HashMap<>();
		List<User> userList = userService.findUserList(); //取得用户列表数据
	
		map.put("userList",userList);
		map.put("status", "1");
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/userListByName")
	public Map<String,Object> userListByName(String txtQuery) {
		List<User> userList = userService.findUserByName(txtQuery); //
		Map<String,Object> map = new HashMap<>();
		map.put("userList",userList);
		map.put("status", "1");
		
		return map;
	}
	
//	@RequestMapping("/toAdd")
//	public ModelAndView toAdd() {
//		ModelAndView mav = new ModelAndView();
//		List<Dept> deptList = deptService.findDeptList();
//		mav.addObject("deptList", deptList); 
//		mav.setViewName("userManage/userAdd.jsp");		
//		return mav;
//	}
	
//	@RequestMapping("/doAdd")
//	public ModelAndView doAdd(User user,HttpSession session) {	
//		String account = user.getAccount();
//		User addUser = userService.findUserByAccount(account);
//		
//		if(addUser!=null) {
//			//账户存在
//			ModelAndView mav = toAdd();
//			mav.addObject("attrInfoUserAdd","账户已存在");
//			return mav;
//		}
//		else {
//			//账户不存在
//			userService.insertUser(user,session);
//			return userList(user);
//		}
//	
//	}
	
//	@RequestMapping("/toUpdate")
//	public ModelAndView toUpdate(Integer id,HttpSession session) {
//		ModelAndView mav = new ModelAndView();
//		List<Dept> deptList = deptService.findDeptList();
//		User user = userService.findUserById(id);
//		mav.addObject("deptList", deptList); 
//		mav.addObject("user", user); 
//		mav.setViewName("userManage/userUpdate.jsp");		
//		return mav;
//	}
	
//	@RequestMapping("/doUpdate")
//	public ModelAndView doUpdate(User user,HttpSession session) {	
//			//System.out.println(user);
//			userService.updateUser(user,session);
//			return userList(user);
//
//	}
	
//	@RequestMapping("/doDelete")
//	public ModelAndView doDelete(User user,HttpSession session) {	
//			//System.out.println(user);
//			userService.deleteUser(user,session);
//			return userList(user);
//
//	}
}
